<?php 
/**
 * 车主管理控制器
 * @Author    紫梦
 * @DateTime  2017-11-02
 * @copyright 响响租车
 * @version   1.0
 * @UpdateTime 2017-11-08
 */
namespace app\index\controller;
use \think\Request;
use app\common\model\Company;
use app\common\model\Personal;
use app\common\model\Franchisee;

class Member extends Base
{
	/**
	 * 共享公司
	 */
	public function index()
	{
		$model = Company::All();
		// $data = $model->toArray();
		// dump($model);
		$this->assign('data',$model);
		return $this->fetch();
	}

	/**
	 * 百度地图选址
	 */
	public function baiDuMap()
	{
		return $this->fetch('bai_du_map');
	}

	/**
	 * 经销商列表
	 */
	public function franchisee()
	{
		$model = Franchisee::All();
		$this->assign('data',$model);
		return $this->fetch();
	}

	/**
	 * 删除经销商
	 */
	public function franchiseeDel($id)
	{
		$msg['status'] = 0;
		$res = Franchisee::destroy($id);
		if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
		
		return $msg;
	}

	// 编辑经销商
	public function franchiseeEdit($id)
	{
		$request = Request::instance();

		if($request->isAjax()){
			$msg['status'] = 0;
			$data = $request->post();
			
			$model = new Franchisee();
			unset($data['uploadfile']);
			// 文件上传
			$file = $request->file('uploadfile');

			if($file)
			{
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
		            // 成功上传后 获取上传信息
		            $data['uploadfile'] = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $msg['status'] = 1;
		            $msg['msg'] = $file->getError();

		            return $msg;
		        }
			}
			$res = $model->allowField(true)->update($data);

			if($res){

				$msg['msg'] = '编辑成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = '编辑失败,请稍后再试';
			}

			return $msg;
		}else{

			$model = Franchisee::get($id);
			
			
			$this->assign('data',$model);
			
			return $this->fetch('franchisee_edit');

		}
	}

	/**
	 * 添加经销商
	 */
	public function franchiseeAdd()
	{
		$request = Request::instance();

		if($request->isAjax()){
			$msg['status'] = 0;
			$data = $request->post();

			$model = new Franchisee();
			$file = $request->file('uploadfile');

						// 文件上传
			if($file)
			{
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
		            // 成功上传后 获取上传信息
		            $data['uploadfile'] = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $msg['status'] = 1;
		            $msg['msg'] = $file->getError();

		            return $msg;
		        }
			}
			
			$res = $model->allowField(true)->save($data);

			if($res){

				$msg['msg'] = '添加成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = '添加失败,请稍后再试';
			}

			return $msg;
		}else{

			return $this->fetch('franchisee_add');

		}
	}

	/**
	 * 共享个人
	 */
	public function list()
	{

		$model = Personal::All();
		$ps_type = config('ps_type');
		$this->assign('data',$model);
		$this->assign('ps_type',$ps_type);
		return $this->fetch();
	}

	/**
	 * 编辑共享个个人
	 */
	public function personalEdit($id)
	{	
		$request = Request::instance();


		if($request->isAjax())
		{
			$msg['status'] = 0;
			$data = $request->post();
			unset($data['uploadfile']);

			// //公司名是否重复
			// $name  = Company::get(['cp_name' => $data['cp_name']]);
			// if($name)
			// {
			// 	$msg['msg'] = '公司名已经存在';
			// 	$msg['status'] = 1;

			// 	return $msg;
			// }
			// unset($name);

			$file = $request->file('uploadfile');
			$model = new Personal();

			// 文件上传
			if($file)
			{
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
		            // 成功上传后 获取上传信息
		            $data['uploadfile'] = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $msg['status'] = 1;
		            $msg['msg'] = $file->getError();

		            return $msg;
		        }
			}


			// $model->data($data);
			$res = $model->allowField(true)->save($data,['ps_id' => $data['id']]);

			if($res){

				$msg['msg'] = '修改成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = $res;
			}

			return $msg;
					
		}else{

			$model = Personal::get($id);
			// dump($model);
			$this->assign('data',$model);
			return $this->fetch('personal_edit');
		}
	}

	/**
	 * 添加共享个人
	 */
	public function personalAdd()
	{
		$request = Request::instance();


		if($request->isAjax())
		{
			$msg['status'] = 0;
			$data = $request->post();
			//用户名是否重复
			$name  = Personal::get(['ps_name' => $data['ps_name']]);
			if($name)
			{
				$msg['msg'] = '该姓名已经存在';
				$msg['status'] = 1;

				return $msg;
			}
			unset($name);

			$file = $request->file('uploadfile');
			$model = new Personal();

			// 文件上传
			if($file)
			{
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
		            // 成功上传后 获取上传信息
		            $data['uploadfile'] = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $msg['status'] = 1;
		            $msg['msg'] = $file->getError();

		            return $msg;
		        }
			}


			$model->data($data);
			$res = $model->allowField(true)->save();

			if($res){

				$msg['msg'] = '添加成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = $res;
			}

			return $msg;
					
		}else{

			return $this->fetch('personal_add');
		}
	}

	/**
	 * 共享公司编辑
	 */
	public function companyEdit($id)
	{
		$request = Request::instance();


		if($request->isAjax())
		{
			$msg['status'] = 0;
			$data = $request->post();
			unset($data['uploadfile']);

			// //公司名是否重复
			// $name  = Company::get(['cp_name' => $data['cp_name']]);
			// if($name)
			// {
			// 	$msg['msg'] = '公司名已经存在';
			// 	$msg['status'] = 1;

			// 	return $msg;
			// }
			// unset($name);

			$file = $request->file('uploadfile');
			$model = new Company();

			// 文件上传
			if($file)
			{
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
		            // 成功上传后 获取上传信息
		            $data['uploadfile'] = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $msg['status'] = 1;
		            $msg['msg'] = $file->getError();

		            return $msg;
		        }
			}


			// $model->data($data);
			$res = $model->allowField(true)->save($data,['cp_id' => $data['id']]);

			if($res){

				$msg['msg'] = '修改成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = $res;
			}

			return $msg;
					
		}else{

			$model = Company::get($id);
			// dump($model);
			$this->assign('data',$model);
			return $this->fetch('company_edit');
		}
	}

	/**
	 * 添加共享公司
	 */
	public function companyAdd()
	{
		$request = Request::instance();


		if($request->isAjax())
		{
			$msg['status'] = 0;
			$data = $request->post();

			//公司名是否重复
			$name  = Company::get(['cp_name' => $data['cp_name']]);
			if($name)
			{
				$msg['msg'] = '公司名已经存在';
				$msg['status'] = 1;

				return $msg;
			}
			unset($name);

			$file = $request->file('uploadfile');
			$model = new Company();

			// 文件上传
			if($file)
			{
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
		            // 成功上传后 获取上传信息
		            $data['uploadfile'] = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $msg['status'] = 1;
		            $msg['msg'] = $file->getError();

		            return $msg;
		        }
			}


			$model->data($data);
			$res = $model->allowField(true)->save();

			if($res){

				$msg['msg'] = '添加成功';
				
			}else{

			    $msg['status'] = 1;
				$msg['msg'] = $res;
			}

			return $msg;
					
		}else{

			return $this->fetch('company_add');
		}
	}
}